<template>
  <div class="page-search">
    <hy-form ref="hyFromRef" v-bind="SearchFormConfig" v-model="formData">
      <template #header>
        <h1>高级检索</h1>
      </template>
      <template #footer>
        <div class="form-btns">
          <el-button icon="el-icon-refresh" @click="handleResetClick"
            >重置</el-button
          >
          <el-button type="primary" @click="submit" icon="el-icon-search"
            >搜索</el-button
          >
        </div>
      </template>
    </hy-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue'
import HyForm from '@/base-ui/form'
export default defineComponent({
  name: 'PageSearch',
  props: {
    SearchFormConfig: {
      type: Object,
      required: true
    }
  },
  components: {
    HyForm
  },
  emits: ['handleQuerySubmit', 'handleReset'],
  setup(props, { emit }) {
    const formItems = props.SearchFormConfig?.formItems ?? []
    const formOriginData: any = {}
    for (const item of formItems) {
      formOriginData[item.key] = ''
    }
    const formData = ref(formOriginData)
    const hyFromRef = ref<InstanceType<typeof HyForm>>()
    // 表单提交
    const submit = () => {
      emit('handleQuerySubmit', formData.value)
    }
    // 表单重置
    const handleResetClick = () => {
      hyFromRef.value?.resetForm()
      emit('handleReset')
    }
    return {
      hyFromRef,
      formData,
      submit,
      handleResetClick
    }
  }
})
</script>

<style lang="less" scoped>
.form-btns {
  text-align: right;
  padding-bottom: 20px;
  padding-right: 10px;
}
</style>
